var docEle = document.documentElement
var clientWidth = docEle.clientWidth;
var btn = document.getElementsByClassName("zm-btn")[0]
var contWrite = document.getElementsByClassName('foot-footer')[0]
var contBody = document.getElementsByClassName('foot-body')[0]
var mtk = document.getElementsByClassName('bg-aglon-wrp')[0]
var aglon = document.getElementsByClassName('aglon')[0]

btn.onclick = function () {
  var bgel = document.getElementsByClassName('app-wrp')[0]
  var formEl = document.getElementsByClassName('zm-form')[0]
  var titleEl = document.getElementsByClassName('title')[0]
 
  var inpEls = document.getElementsByClassName('zm-inp')
  var divEl = document.createElement('div')
  titleEl.classList.add('sta-sub')
  divEl.classList.add('sta-info')
  divEl.innerHTML = "欢迎加入无限IPLab，期待我们的故事无限发生！同时烦请您将IP的相关资料，发送至我们的公司邮箱：<span style='color:red;'>service@yc1710.com</span>，以便我们进一步了解您的IP，我们将尽快跟您联系"
  if (clientWidth>=750) {
    titleEl.style.marginTop="0.88rem"
    divEl.style.top = '3rem'
    divEl.style.fontSize = '0.2rem'
  }
  //是否有空
  for(var i = 0; i < inpEls.length; i++){
    console.log(inpEls[i].value)
    var unNull = inpEls[i].value === ""
    if (unNull) { // 有空出现提示 退出方法 不继续执行
      mtk.style.display = 'block'
      aglon.style.display = 'block'
      if (clientWidth>=750) {
        aglon.style.height = '260px'
      } else {
        aglon.style.height = '3.033rem'
      }
      contBody.innerText= "您有内容未填写，请完整填写后再提交！"
      return
    }
  }
  // 验证电话号码
  
  var phone = inpEls[1].value;
  if (!(/^[1][3,4,5,7,8][0-9]{9}$/.test(phone))) {
    mtk.style.display = 'block'
    aglon.style.display = 'block'
    if (clientWidth>=750) {
      aglon.style.height = '240px'
    } else {
        aglon.style.height = '2.7rem'
      }
    contBody.innerText= "手机号码有误，请重填!"
    return
  }  
  console.log('111')
  //  没空 且验证电话号码  继续执行 提交
   var datas = {  //数据
    "createTime": '',
    "ip": inpEls[2].value,
    "ipDescription": inpEls[3].value,
    "phone": inpEls[1].value,
    "username": inpEls[0].value
  }
  var datas = JSON.stringify(datas)
  console.log(datas)
  var XHR = new XMLHttpRequest();
  XHR.open('POST', "http://192.168.0.134:8080/dealer/form/public/saveForm");
  XHR.setRequestHeader("accept", "application/json, text/plain, */*"); 
  XHR.setRequestHeader("Content-Type", "application/json"); 
  XHR.send(datas);
  XHR.onreadystatechange = function () {
    if (XHR.readyState === 4) {
      if((XHR.status >= 200 && XHR.status < 300) || XHR.status == 300) {
        //  提交成功
        bgel.id = 'app-wrp-sbum' //背景
        formEl.innerHTML = '',  //清空form
        formEl.appendChild(divEl) // 提示成功
      } else {
       alert("Request was unsuccessful: " + XHR.status);
      } 
    }
  }
}

contWrite.onclick = function () {
  mtk.style.display = 'none'
  aglon.style.display = 'none'
}